﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 
using HumanWorkflow.Framework;
using HumanWorkflow.SemanticModel;

namespace OrderWorkflow.OrderWorkflowWithExternalDsl 
{
    public partial class OrderWorkflowWithExternalDsl: HumanWorkflowEngine
    {
        public OrderWorkflowWithExternalDsl()
            : base(CustomInitializeSemanticModel())
        {
        }

        static HumanWorkflowSemanticModel CustomInitializeSemanticModel()
        {
            HumanWorkflowSemanticModel semanticModel = OrderWorkflowWithExternalDsl.InitializeSemanticModel();

            FinalStep finalStep = (FinalStep)((IList<Step>)semanticModel.Steps).Where<Step>(s => s is FinalStep).First<Step>();

            foreach (Step step in semanticModel.Steps)
            {
                if (! (step is FinalStep))
                {
                    semanticModel.AddTransition(step.Name, finalStep.Name, step.Name + "StopWorkflow");
                }
            
            }

            return semanticModel;
        }

        public bool StocksAreOk { get; set; }

        public override string ExecuteAutomaticStep(Step executedStep)
        {
            string nextTransitionName;

            switch (executedStep.Name)
            {
                case OrderWorkflowConstants.Steps.AreAllItemsOnStockDecisionStep:
                    if (StocksAreOk)
                    {
                        nextTransitionName = OrderWorkflowConstants.Transitions.ItemsAreAvailable;
                    }
                    else
                    {
                        nextTransitionName = OrderWorkflowConstants.Transitions.ItemsAreMissing;                 
                    }

                    break;
                case OrderWorkflowConstants.Steps.SendNotificationEmailToCustomerAutomaticStep:
                    
                    //TODO: send an email here to the customer

                    nextTransitionName = OrderWorkflowConstants.Transitions.EmailSent;
                    break;

                default:
                    throw new NotImplementedException(executedStep.Name + " is not implemented in ExecuteAutomaticStep override.");

            }

            return nextTransitionName;
        }

    }
}
